package template.page.user;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.Column;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;

import template.page.BorderPage;

@SuppressWarnings("serial")
public class HighScores extends BorderPage {

	public String title = "High Scores";
	private Table scoreTable = new Table("scoreTable");

	@SuppressWarnings("rawtypes")
	public HighScores() {
		addControl(scoreTable);
		// Setup table
		scoreTable.setClass(Table.CLASS_ITS);
		scoreTable.setPageSize(50);
		scoreTable.setShowBanner(true);
		scoreTable.setSortable(true);
		
		scoreTable.addColumn(new Column("gameName"));
		scoreTable.addColumn(new Column("points"));
		scoreTable.addColumn(new Column("playerName"));		

		// Table rowList will be populated through a DataProvider which loads
		// data on demand.
		scoreTable.setDataProvider(new DataProvider() 
		{
			public List getData() 
			{
				return getGameSessionService().getAllScores();
			}
		});

		// Below we setup the table to preserve it's state (sorting and paging)
		// while editing customers
		scoreTable.getControlLink().setActionListener(new ActionListener() {

			public boolean onAction(Control source) {
				// Save Table sort and paging state between requests.
				// NOTE: we set the listener on the table's Link control which
				// is invoked
				// when the Link is clicked, such as when paging or sorting.
				// This ensures the table state is only saved when the state
				// changes, and
				// cuts down on unnecessary session replication in a cluster
				// environment.
				scoreTable.saveState(getContext());
				return true;
			}
		});

		// Restore the table sort and paging state from the session between
		// requests
		scoreTable.restoreState(getContext());
	}
}
